<template>
  <a-modal
    width="900px"
    :bodyStyle="{ height: '550', overflow: 'auto' }"
    :visible="modalVisible"
    @cancel="handleCancel"
    title="干预行政复议活动记录"
  >
    <a-spin :spinning="spinning" tips="加载中">
      <a-descriptions title="干预行政复议活动记录表" class="label" layout="horizontal" :column="1" bordered>
        <a-descriptions-item label="案  号" style="max-width: 150px;">
          <span class="beyond_hiding">{{ (mainInfoCase && mainInfoCase.acceptcode) ? mainInfoCase.acceptcode : '' }} </span>
        </a-descriptions-item>
        <a-descriptions-item label="申请人" style="max-width: 150px;">
          <span class="beyond_hiding">{{ (mainInfoCase && mainInfoCase.applicantName) ? mainInfoCase.applicantName : '' }}</span>
        </a-descriptions-item>
        <a-descriptions-item label="被申请人" style="max-width: 150px;">
          <span class="beyond_hiding">{{ (mainInfoCase && mainInfoCase.targetName) ? mainInfoCase.targetName : '' }}</span>
        </a-descriptions-item>
        <a-descriptions-item label="第 三 人" style="max-width: 150px;">
          <span class="beyond_hiding">{{ (mainInfoCase && mainInfoCase.thirderName) ? mainInfoCase.thirderName : '' }}</span>
        </a-descriptions-item>
        <a-descriptions-item label="案    由" style="max-width: 150px;">
          <span class="beyond_hiding">{{ (mainInfoCase && mainInfoCase.case) ? mainInfoCase.case : '' }}</span>
        </a-descriptions-item>
      </a-descriptions>

      <div style="margin: 10px 10px">
        <CaseDecisionInterveneForm
          v-if="initComponent"
          ref="mainCsaeDecisionForm"
          :faapplyid="faapplyid"
          :applyid="applyid"
          :isMain="true"
          :isDiasbled="!isMain"
          :recordObj="mainRecordObj"></CaseDecisionInterveneForm>
      </div>

      <div style="margin: 10px 10px">
        <CaseDecisionInterveneForm
          ref="subCsaeDecisionForm"
          v-if="initComponent && !isMain"
          :faapplyid="faapplyid"
          :applyid="applyid"
          :isMain="false"
          :isDiasbled="isMain"
          :recordObj="subRecordObj"></CaseDecisionInterveneForm>
      </div>

    </a-spin>
    <template slot="footer">
      <div style="display: flex; justify-content: center">
        <a-button style="margin-right: 20px" @click="handleCancel"> 取消 </a-button>
        <a-button type="primary" @click="onSubmit" :loading="loading"> 确定</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import storage from 'store'
import { APP_USER, APP_DEPT } from '@/store/mutation-types'
import { saveRecord, getNoDecisionRecords, getNoDecisionMainRecords, getNoDecisionSubRecords, getCaseMainInfo } from '@/api/accept/apl_decision_record'
export default {
  props: {
    faapplyid: {
      type: String,
      required: false
    },
    applyid: {
      type: String,
      required: false
    },
    // 是否主办人
    isMain: {
      type: Boolean,
      required: true
    },
    // 是否流程中
    isFlow: {
      type: Boolean,
      required: true
    }
  },
  components: {
    CaseDecisionInterveneForm: () => import('@/views/handleCase/accept/CaseDecisionInterveneForm.vue')
  },
  data () {
    return {
      labelCol: { span: 4 }, // label宽
      wrapperCol: { span: 20 }, // 组件宽
      currentUser: storage.get(APP_USER), // 登录人信息
      currentDept: storage.get(APP_DEPT), // 登录人部门
      loading: false, // 按钮显示
      modalVisible: false, // 模态框显示
      mainRecordObj: {},
      subRecordObj: {},
      initComponent: false,
      mainInfoCase: {
        acceptcode: undefined, // 案号
        targetName: undefined, // 被申请人名称
        applicantName: undefined, // 申请人名称
        thirderName: undefined, // 第三人名称
        case: undefined // 案由
      },
      spinning: false
    }
  },
  created () {},
  methods: {
    async init () {
      this.modalVisible = true
      this.initComponent = false
      let res
      let noDecisionRecord = []
      // 查询案件的基本信息
      this.spinning = true
      try {
        const mainRes = await getCaseMainInfo(this.faapplyid)
        if (mainRes.code == 0) {
          const mainResult = mainRes.data
          if (mainResult) {
            if (mainResult.mainInfo) {
              this.mainInfoCase.acceptcode = mainResult.mainInfo.acceptcode
            }
            if (mainResult.admreview) {
              this.mainInfoCase.case = mainResult.admreview.reason
            }
            if (mainResult.applicantList && mainResult.applicantList.length > 0) {
              const applicantName = mainResult.applicantList.map(el => el.name).join(',')
              this.mainInfoCase.applicantName = applicantName
            }
            if (mainResult.agentList && mainResult.agentList.length > 0) {
              const agentName = mainResult.agentList.map(el => el.name).join(',')
              this.mainInfoCase.agentName = agentName
            }
            if (mainResult.targetList && mainResult.targetList.length > 0) {
              const targetName = mainResult.targetList.map(el => el.name).join(',')
              this.mainInfoCase.targetName = targetName
            }
            if (mainResult.thirdperList && mainResult.thirdperList.length > 0) {
              const thirderName = mainResult.thirdperList.map(el => el.name).join(',')
              this.mainInfoCase.thirderName = thirderName
            }
          }
        } else {
          this.mainInfoCase = {}
        }
        if (!this.isFlow) {
          res = await getNoDecisionRecords(this.faapplyid)
          if (res.code == 0) {
            noDecisionRecord = res.data
          }
        } else {
          if (this.isMain) {
            res = await getNoDecisionMainRecords(this.faapplyid)
            if (res.code == 0) {
              noDecisionRecord = res.data
            }
          } else {
            const res1 = await getNoDecisionMainRecords(this.faapplyid, this.applyid)
            if (res1.code == 0) {
              noDecisionRecord = [...noDecisionRecord, ...res1.data]
            }
            const res2 = await getNoDecisionSubRecords(this.faapplyid, this.applyid)
            if (res2.code == 0) {
              noDecisionRecord = [...noDecisionRecord, ...res2.data]
            }
          }
        }
        if (noDecisionRecord && noDecisionRecord.length > 0) {
          const filterArray = noDecisionRecord.filter(el => el.signtype == 3200)
          if (filterArray && filterArray.length > 0) {
            this.mainRecordObj = filterArray[0]
          } else {
            this.mainRecordObj = {}
          }
          if (!this.isMain) {
            const filterArray = noDecisionRecord.filter(el => el.signtype == 3201)
            if (filterArray && filterArray.length > 0) {
              this.subRecordObj = filterArray[0]
            } else {
              this.subRecordObj = {}
            }
          }
        } else {
          this.mainRecordObj = {}
          this.subRecordObj = {}
        }
      } finally {
        this.spinning = false
        this.initComponent = true
      }
    },
    handleCancel () {
      this.modalVisible = false
    },
    async onSubmit () {
      // 判断是否表单填写完成
      let refs
      if (this.isMain) {
        refs = this.$refs.mainCsaeDecisionForm
      } else {
        refs = this.$refs.subCsaeDecisionForm
      }
      const validate = await refs.validate()
      if (refs && validate) {
        const formData = await refs.getFormData()
        saveRecord(formData).then(res => {
          if (res.code !== 0) {
            this.$message.error('保存失败')
          } else {
            this.$message.success('保存成功')
            this.handleCancel()
            this.$emit('submit', formData.id)
          }
        }).finally(() => { this.loading = false })
      } else {
        this.$message.error('保存失败,请检查表单是否填写完成')
      }
    }
  }
}
</script>

<style lang="less" scoped>

::v-deep .ant-descriptions-title {
  text-align: center;
}
::v-deep .ant-descriptions-item-label{
  width: 120px !important;
}

.label .ant-descriptions-item-label{
  width: 120px !important;
}
.content-label {
  color: rgba(0, 0, 0, 0.85);
  font-weight: bold;
  font-size: 16px;
  line-height: 1.5;
  text-align: center;
}
</style>
